package com.xuelang.pipeline.app.resource.impl;

import cn.hutool.core.util.IdUtil;
import com.xuelang.jadp.client.sys.dto.UserInfoDTO;
import com.xuelang.pipeline.app.resource.EnvironmentService;
import com.xuelang.pipeline.app.resource.converter.EnvironmentDTOToEnvironmentConverter;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.resource.dto.EnvironmentDTO;
import com.xuelang.pipeline.client.resource.dto.EnvironmentQuery;
import com.xuelang.pipeline.domain.application.service.ApplicationDomainService;
import com.xuelang.pipeline.domain.pipeline.service.PipelineDomainService;
import com.xuelang.pipeline.domain.resource.Environment;
import com.xuelang.pipeline.domain.resource.service.EnvironmentDomainService;
import com.xuelang.sys.app.sys.UserManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 环境服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Service
public class EnvironmentServiceImpl implements EnvironmentService {

    @Autowired
    EnvironmentDomainService environmentDomainService;
    @Autowired
    ApplicationDomainService applicationDomainService;
    @Autowired
    PipelineDomainService pipelineDomainService;
    @Autowired
    UserManagerService userManagerService;

    private EnvironmentDTO fullfillUpdater(EnvironmentDTO environmentDTO){
        UserInfoDTO user = userManagerService.getUserById(environmentDTO.getUpdaterId());
        if (Objects.nonNull(user)) {
            environmentDTO.setUpdater(user.getRealName());
        }else{
            environmentDTO.setUpdater(Constant.UNKNOWN_USER);
        }
        return environmentDTO;
    }

    @Override
    public PageRecord<EnvironmentDTO> pageList(PageParam<EnvironmentQuery> dtoPageParam) {
        EnvironmentQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new EnvironmentQuery() : condition;
        PageRecord<Environment> pageRecord = environmentDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream()
                        .map(EnvironmentDTOToEnvironmentConverter.INSTANCE::environmentToEnvironmentDTO)
                        .map(this::fullfillUpdater)
                        .collect(Collectors.toList())
        );
    }

    @Override
    public List<EnvironmentDTO> list(EnvironmentQuery environmentQuery) {
        return environmentDomainService.list(environmentQuery).stream()
                .map(EnvironmentDTOToEnvironmentConverter.INSTANCE::environmentToEnvironmentDTO)
                .map(this::fullfillUpdater)
                .collect(Collectors.toList()
                );
    }


    @Override
    public Boolean add(EnvironmentDTO environmentDTO) {
        BizAssert.notNull(environmentDTO.getApplicationCode(), "应用编码不能为空");
        BizAssert.notNull(environmentDTO.getCode(), "环境编码不能为空");
        BizAssert.notNull(environmentDTO.getName(), "环境名称不能为空");
        BizAssert.isTrue(applicationDomainService.existsCode(environmentDTO.getApplicationCode()), "应用编码不存在");
        Environment environment = EnvironmentDTOToEnvironmentConverter.INSTANCE.environmentDTOToEnvironment(environmentDTO);
        environment.setUuid(IdUtil.fastSimpleUUID());
        return environmentDomainService.save(environment);
    }

    @Override
    public Boolean update(EnvironmentDTO environmentDTO) {
        BizAssert.notNull(environmentDTO.getUuid(), "环境唯一标识不能为空");
        BizAssert.notNull(environmentDTO.getApplicationCode(), "应用编码不能为空");
        Environment environment = EnvironmentDTOToEnvironmentConverter.INSTANCE.environmentDTOToEnvironment(environmentDTO);
        return environmentDomainService.update(environment);
    }

    @Override
    public Boolean delete(String uuid) {
        BizAssert.notNull(uuid, "环境唯一标识不能为空");
        return environmentDomainService.delete(uuid);
    }

    @Override
    public EnvironmentDTO query(String uuid) {
        BizAssert.notNull(uuid, "环境唯一标识不能为空");
        return EnvironmentDTOToEnvironmentConverter.INSTANCE.environmentToEnvironmentDTO(environmentDomainService.query(uuid));
    }

    @Override
    public Boolean existsPipelineByEnvironment(String environmentUuid) {
        BizAssert.notNull(environmentUuid, "环境唯一标识不能为空");
        return pipelineDomainService.existsByEnvironmentUuid(environmentUuid);
    }

}
